<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
	protected $_id;
    protected $_isAdmin = false;
    public $role_id;
	/**
	 * Authenticates a user.
	 * The example implementation makes sure if the username and password
	 * are both 'demo'.
	 * In practical applications, this should be changed to authenticate
	 * against some persistent user identity storage (e.g. database).
	 * @return boolean whether authentication succeeds.
	 */
	public function authenticate()
	{
		$record = User::model()->findByAttributes(array('email' => $this->username));

		if($record === null)
        {
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        }
        else if(trim($record->password) != md5(trim($this->password)))
        {
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
        }
        else if($record->status==0 )
        {
            $this->errorCode=  self::ERROR_USERNAME_BLOCKED;
        }
        else
        {
            $this->_id = $record->id;
            $this->_isAdmin = false;
            $this->errorCode = self::ERROR_NONE;
            Yii::app()->session['LOGGED_USER'] = $record;
        }

        return !$this->errorCode;
	}

    public function getId()
    {
        return $this->_id;
    }

    public function getIsAdmin()
    {
        return $this->_isAdmin;
    }

    public function getRoleId()
    {
        return $this->role_id;
    }
}